<!--
 * @Description: 一级渠道信息编辑
 * @Version: 1.0
 * @Author: Yuanjy
 * @Date: 2020-04-14 21:31:58
 * @LastEditors: Yuanjy
 * @LastEditTime: 2020-04-18 17:57:45
 -->
<template>
  <el-dialog :append-to-body="true" :visible.sync="dialog" :title="isAdd ? '新增一级渠道信息' : '编辑一级渠道信息'" width="700px">
    <el-form ref="form" :model="form" label-position="left" label-width="70px" class="small-space" style="width: 90%; margin-left:0px;">
      <el-form-item label="渠道名称">
        <el-input v-model="form.name" placeholder="请填写渠道名称" />
      </el-form-item>
      <el-form-item label="渠道编码">
        <span v-text="form.code" />
      </el-form-item>
      <el-form-item label="描述">
        <el-input v-model="form.description" type="textarea" placeholder="请填写渠道描述" />
      </el-form-item>
      <el-form-item label="状态">
        <el-switch v-model="form.status" active-color="#13ce66" inactive-color="#ff4949" active-value="1" inactive-value="0" active-text="启用" inactive-text="禁用" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancel">取 消</el-button>
      <el-button :loading="formSubmiting" type="primary" @click="doSubmit()">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import jsonEditor from '@/views/components/jsonEditor'
import { isJson } from '@/utils'

export default {
  components: { jsonEditor },
  props: {
    isAdd: {
      type: Boolean,
      required: true
    },
    sup_this: {
      type: Object,
      default: null
    }
  },
  data() {
    return {
      loading: false,
      dialog: false,
      formSubmiting: false,
      form: {
        id: null,
        code: null,
        name: null,
        level: 1,
        parent_id: null,
        qn_url: '',
        wechat_info: '',
        status: '1',
        description: '',
        reply: '',
        config: '',
        page_type: ''
      },
      firstIsDisable: false,
      secondIsDisable: false,
      activeTabName: 'first',
      jsonEditor: false,
      isJsonWechat: false,
      isJson: false,
      topChannels: {}
    }
  },
  mounted() {
    this.form.code = this._generateCode()
  },
  methods: {
    cancel() {
      this.resetForm()
    },
    doSubmit() {
      this.loading = true
      if (isJson(this.form.wechat_info)) {
        this.form.wechat_info = JSON.stringify(this.form.wechat_info)
      }
      if (isJson(this.form.config)) {
        this.form.config = JSON.stringify(this.form.config)
      }
      if (this.isAdd) {
        this.doAdd()
      } else this.doEdit()
    },
    doAdd() {
      this.$api.enterprise.channel
        .add({
          data: this.form
        }).then(res => {
          this.resetForm()
          this.$notify({
            title: '添加成功',
            type: 'success',
            duration: 2500
          })
          this.loading = false
          this.$parent.$parent.init()
        }).catch(err => {
          this.loading = false
          console.log(err.response.data.message)
        })
    },
    doEdit() {
      this.$api.enterprise.channel
        .edit({
          data: this.form
        }).then(res => {
          this.resetForm()
          this.$notify({
            title: '修改成功',
            type: 'success',
            duration: 2500
          })
          this.loading = false
          this.sup_this.init()
        }).catch(err => {
          this.loading = false
          console.log(err.response.data.message)
        })
    },
    _generateCode() {
      return (new Date()).getTime().toString(16).toUpperCase()
    },
    resetForm() {
      this.dialog = false
      this.$refs['form'].resetFields()
      this.form = {
        id: null,
        code: this._generateCode(),
        name: null,
        level: 1,
        parent_id: null,
        qn_url: '',
        wechat_info: '',
        status: '1',
        description: '',
        reply: '',
        config: '',
        page_type: ''
      }
    }
  }
}
</script>

<style scoped>

</style>
